package com.yanq.liancar.activity;

import android.content.res.Resources;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.text.TextUtils;
import android.util.TypedValue;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.google.gson.Gson;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;
import com.vondear.rxtool.RxActivityTool;
import com.vondear.rxtool.view.RxToast;
import com.yanq.liancar.R;
import com.yanq.liancar.activity.base.BaseToolbarActivity;
import com.yanq.liancar.adapter.OrderPagerAdapter;
import com.yanq.liancar.bean.BaseBean;
import com.yanq.liancar.bean.BqueryShopAllTypeBean;
import com.yanq.liancar.bean.FullServiceBean;
import com.yanq.liancar.bean.ShopAllServiceTypeBean;
import com.yanq.liancar.bean.UserBean;
import com.yanq.liancar.fragment.BOrderFragment;
import com.yanq.liancar.fragment.BSreviceAdmFragment;
import com.yanq.liancar.url.Config;
import com.yanq.liancar.util.Utils;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class SmallBSreviceAdmActivity extends BaseToolbarActivity {
    @BindView(R.id.tab_layout)
    TabLayout mTabLayout;
    @BindView(R.id.viewPager)
    ViewPager viewPager;
    @BindView(R.id.ll_addService)
    LinearLayout tvAddService;
    @BindView(R.id.tv_noService)
    TextView noService;
    private List<String> mList = new ArrayList<>();
    private List<Integer> typeNum = new ArrayList<>();
    private List<ShopAllServiceTypeBean.DataBean> mDataBeanList = new ArrayList<>();

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.small_b_sreviceadm);
        Utils.setStatusBar(this, true,false);
        ButterKnife.bind(this);
        mTvTitle.setText("服务管理");
    }

    @Override
    protected void onResume() {
        super.onResume();
        initOkGo();
    }

    @OnClick(R.id.ll_addService)
    public void onViewClicked() {
        //RxActivityTool.skipActivity(mContext, SmallBAddSreviceActivity.class);
        //RxActivityTool.skipActivity(mContext, SmallBServiceDescriptionActivity.class);
        RxActivityTool.skipActivity(mContext, SmallBAddServiceActivity.class);
    }


    private void initViewPager() {
        // 创建一个集合,装填Fragment
        List<Fragment> fragments = new ArrayList<>();
        for (int i = 0; i < typeNum.size(); i++) {
            fragments.add(BSreviceAdmFragment.newInstance(typeNum.get(i)));
        }
        // 装填
//        fragments.add(BSreviceAdmFragment.newInstance(1));
//        fragments.add(BSreviceAdmFragment.newInstance(2));
//        fragments.add(BSreviceAdmFragment.newInstance(3));
//        fragments.add(BSreviceAdmFragment.newInstance(4));
//        fragments.add(BSreviceAdmFragment.newInstance(5));
//        fragments.add(BSreviceAdmFragment.newInstance(6));

//        titles = new ArrayList<>();
//        titles.add("精致洗车");
//        titles.add("标准洗车");
//        titles.add("美容养护");
//        titles.add("到店保养");
//        titles.add("钣金喷漆");
//        titles.add("代办服务");
        // 创建ViewPager适配器
        OrderPagerAdapter myPagerAdapter = new OrderPagerAdapter(getSupportFragmentManager(), fragments, mList);

        // 给ViewPager设置适配器
        viewPager.setAdapter(myPagerAdapter);
        // TabLayout 指示器 (记得自己手动创建4个Fragment,注意是 app包下的Fragment 还是 V4包下的 Fragment)
       /* mTabLayout.addTab(mTabLayout.newTab().setText(titles.get(0)), true);
        mTabLayout.addTab(mTabLayout.newTab().setText(titles.get(1)));
        mTabLayout.addTab(mTabLayout.newTab().setText(titles.get(2)));
        mTabLayout.addTab(mTabLayout.newTab().setText(titles.get(3)));*/
        // 使用 TabLayout 和 ViewPager 相关联
        mTabLayout.setupWithViewPager(viewPager);
        mTabLayout.post(new Runnable() {
            @Override
            public void run() {
                setIndicator(mTabLayout, 10, 10);
            }
        });
    }
    //更改TabLayout下标指示器长度
    public void setIndicator(TabLayout tabs, int leftDip, int rightDip) {
        Class<?> tabLayout = tabs.getClass();
        Field tabStrip = null;
        try {
            tabStrip = tabLayout.getDeclaredField("mTabStrip");
        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        }

        tabStrip.setAccessible(true);
        LinearLayout llTab = null;
        try {
            llTab = (LinearLayout) tabStrip.get(tabs);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }

        int left = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, leftDip, Resources.getSystem().getDisplayMetrics());
        int right = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, rightDip, Resources.getSystem().getDisplayMetrics());

        for (int i = 0; i < llTab.getChildCount(); i++) {
            View child = llTab.getChildAt(i);
            child.setPadding(0, 0, 0, 0);
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(0, LinearLayout.LayoutParams.MATCH_PARENT, 1);
            params.leftMargin = left;
            params.rightMargin = right;
            child.setLayoutParams(params);
            child.invalidate();
        }
    }

    //查看全部服务
    private void initOkGo() {
        if ("".equals(UserBean.getbToken(this)) || TextUtils.isEmpty(UserBean.getbToken(this))) {
            RxToast.error("请先登录");
        } else {
            OkGo.<String>post(Config.s + "llc/queryShopAllServiceType")
                    .tag(this)
                    .params("token", UserBean.getbToken(this))
                    .execute(new StringCallback() {
                        @Override
                        public void onSuccess(Response<String> response) {
                            Gson gson = new Gson();
                            ShopAllServiceTypeBean shopAllServiceTypeBean = gson.fromJson(response.body(), ShopAllServiceTypeBean.class);
                            if (shopAllServiceTypeBean.isIsSuccess()) {
                                mDataBeanList.clear();
                                mDataBeanList=shopAllServiceTypeBean.getData();
                                if (mDataBeanList.size()>0){
                                    mList.clear();
                                    typeNum.clear();
                                    for (int i = 0; i < shopAllServiceTypeBean.getData().size(); i++) {
                                        mList.add(shopAllServiceTypeBean.getData().get(i).getTypeName());
                                        typeNum.add(shopAllServiceTypeBean.getData().get(i).getTypeNum());
                                    }
                                    noService.setVisibility(View.GONE);
                                    mTabLayout.setVisibility(View.VISIBLE);
                                    viewPager.setVisibility(View.VISIBLE);
                                    initViewPager();
                                }else {
                                    noService.setVisibility(View.VISIBLE);
                                    mTabLayout.setVisibility(View.GONE);
                                    viewPager.setVisibility(View.GONE);
                                }
                            }

                        }

                        @Override
                        public void onError(Response<String> response) {
                            super.onError(response);
                            RxToast.error("服务器连接错误");
                        }
                    });
        }
    }
}
